package com.lnj.messages.quarzJob;

import com.lnj.messages.servise.NotificationService;
import com.lnj.utils.OrderMessage;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;

import java.time.LocalDateTime;

public class OrdersJob implements Job {
    @Autowired
    private NotificationService notificationService;

    @Override
    public void execute(JobExecutionContext context)  {
        //context是一个上下文对象，它包含了作业的详细信息，比如作业的名称、作业的组、作业的参数等
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        Long orderId = dataMap.getLong("orderId");
        String action = dataMap.getString("action");

        //kafka消息对象
        OrderMessage event = new OrderMessage();
        event.setOrderId(orderId);
        event.setCreateTime(LocalDateTime.now());

        if ("REMIND".equalsIgnoreCase(action)) {
            // 发送提醒逻辑
            notificationService.sendPaymentRemind(orderId);
            event.setEventType("REMINDED");
        } else if ("CANCEL".equalsIgnoreCase(action)) {
            // 取消订单逻辑
            notificationService.sendOrderCancelled(orderId);
            event.setEventType("CANCELLED");
        }
    }
}
